// Z型布局数据格式
const LEFT_TYPE = 'left'
const RIGHT_TYPE = 'right'
export const getCanvasDataZ = (len = 0) => {
  if (!len)
    return {
      nodes: [],
      edges: []
    }
  const arr = new Array(Math.ceil(len / 2)).fill({})
  const edges = []
  const leftNodes = []
  const rightNodes = []
  const anchorPoints = [
    [0, 0.5],
    [0.5, 0],
    [1, 0.5],
    [0.5, 1]
  ]
  arr.forEach((_, idx) => {
    const lNode = {
      id: `l${idx}`,
      label: `L__${idx}`,
      sort: 2 * (idx + 1) - 1,
      cluster: LEFT_TYPE,
      anchorPoints: anchorPoints,
      username: '',
      deviceName: '',
      deviceSn: '',
      type: 'kingCard'
    }
    leftNodes.push(lNode)
    if (idx < Math.floor(len / 2)) {
      const rNode = {
        id: `r${idx}`,
        label: `R__${idx}`,
        sort: 2 * (idx + 1),
        cluster: RIGHT_TYPE,
        anchorPoints: anchorPoints,
        username: '',
        deviceName: '',
        deviceSn: '',
        type: 'kingCard'
      }
      rightNodes.push(rNode)
      const lEdge = {
        source: lNode.id,
        target: rNode.id,
        style: {
          endArrow: true,
          stroke: '#33a5e2'
        },
        type: 'line',
        sourceAnchor: 2,
        targetAnchor: 0,
        idx: idx,
        autoDraw: false
      }
      edges.push(lEdge)

      if (idx !== arr.length - 1) {
        const rEdge = {
          source: rNode.id,
          target: `l${idx + 1}`,
          type: 'polyline',
          style: {
            endArrow: true,
            stroke: '#33a5e2',
            radius: 4,
            offset: 12
          },
          sourceAnchor: 3,
          targetAnchor: 1,
          idx: idx + 1,
          autoDraw: false
        }
        edges.push(rEdge)
      }
    }
  })
  return {
    nodes: [...leftNodes, ...rightNodes],
    edges: edges
  }
}
